package com.kwlt.smc.rt;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springside.modules.test.spring.SpringTransactionalTestCase;

import com.kwlt.smc.dao.SmsSendDetailDao;
import com.kwlt.smc.entity.SmsSendDetail;
import com.kwlt.smc.util.ConUtils;


@ContextConfiguration(locations = { "/applicationContext.xml" })
public class RtSmsSendDetail extends SpringTransactionalTestCase{
	
	@Autowired
	private SmsSendDetailDao smsSendDetailDao;
	
	@Test
	@Rollback(false)
	public void rtSms(){
		String path = "D:\\home\\rebumtall.txt";
		long begin = System.currentTimeMillis();
		System.out.println("补全数据开始");
		try {
			FileReader fr = new FileReader(path);
            BufferedReader br = new BufferedReader(fr);
			int i=0;
			int countsum = 0;
			int count = 0;
			String str = null;
			List<SmsSendDetail> list = new ArrayList<SmsSendDetail>();
			while((str=br.readLine())!=null){
				i++;
				countsum++;
				System.out.println(countsum+"=======");
				String[] strings = str.split(" ");
				if(strings.length==3){
					String acc = strings[0];
					String phone = strings[1];
					String msgId = strings[2];
					if(acc.length()!=16 || phone.length()!=11 || msgId.length()!=20){
						System.out.println(str);
					}else{
						if(!checkDate(msgId,phone)){
							//插入数据
							String userId = msgId.substring(0,4);
							String date = "201"+msgId.substring(4,15);
							if(!userId.equals("0001")){
								System.out.println(str);
							}else{
								SmsSendDetail sms = getSms(phone,date,"mysteel",msgId);
								count++;
								list.add(sms);
							}
						}
					}
				}else{
					System.out.println(str);
				}
				if(i>1000 && list.size()>0){
					i=0;
					smsSendDetailDao.save(list,"20120926");
					list = new ArrayList<SmsSendDetail>();
				}
			}
			if(list!=null && list.size()>0){
				smsSendDetailDao.save(list,"20120926");
			}
			long end = System.currentTimeMillis();
			System.out.println("总共补全数据"+count+"条");
			System.out.println("总执行时间："+(end-begin));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public SmsSendDetail getSms(String phone,String date,String user,String localId){
		SmsSendDetail po = new SmsSendDetail();
		po.setChannel("sh000");
		po.setPhoneNum(phone);
		po.setUser(user);
		po.setIntSubmitDate(ConUtils.revDate(date, "yyyyMMddHHmmss"));
		po.setLocalMsgId(localId);
		po.setSendDate(ConUtils.revDate(date, "yyyyMMddHHmmss"));
		po.setSendSuccessFlag("0");
		po.setUserDate(date.substring(0,4)+"-"+date.substring(4,6)+"-"+date.substring(6,8));
		po.setOperators("mobile");
		return po;
	}
	
	public boolean checkDate(String msgId,String phone){
		StringBuffer sql = new StringBuffer();
		sql.append("select 1 from sms_send_detail_20120926 a where a.LOCAL_MSG_ID = '"+msgId+"' and a.PHONE_NUMBER = '"+phone+"'");
		List list = smsSendDetailDao.query(sql.toString());
		if(list!=null && list.size()>0){
			return true;
		}
		return false;
	}
}
